package com.kfyang.manualgear.generator.declare;

import com.kfyang.manualgear.annotation.crud.Modify;
import com.kfyang.manualgear.constant.Constants;
import com.kfyang.manualgear.util.GlobalConfigUtils;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;

import static com.kfyang.manualgear.constant.StatementConstants.NEW_SQL_DEFINE;

/**
 * 生成标注了 Modify 注解的方法实现
 */
public abstract class ModifyMethodGenerator extends AbstractGenerator {

    /**
     * 方法是否有返回值
     */
    protected boolean hasReturn;
    protected boolean useSimple;

    /**
     * @param element 修改方法签名
     * @param classWrapper classWrapper
     * @param useSimple 该类是否用到简单方法
     */
    public ModifyMethodGenerator(ExecutableElement element, ClassWrapper classWrapper, boolean useSimple) {
        super(element, classWrapper);

        if (methodInfo.type == null) {
            Modify modifyAnno = element.getAnnotation(Modify.class);
            if (modifyAnno != null) {
                methodInfo.type = Constants.MODIFY;
                methodInfo.sql = modifyAnno.value();
            }
        }
        this.useSimple = useSimple;
    }

    @Override
    protected void init() {
        super.init();
        hasReturn = element.getReturnType().getKind() != TypeKind.VOID;
    }

    @Override
    protected void addSql() {
        methodBuilder.addStatement(NEW_SQL_DEFINE);
        generateSqlAndFrag(methodInfo.sql);
    }

    @Override
    protected boolean notSupport() {
        return (useSimple && GlobalConfigUtils.methodSet.contains(methodName())) ||
            !Constants.MODIFY.equals(methodInfo.type);
    }
}
